//
//  AnimationUtil.m
//  autolayoutAnimationDemo
//
//  Created by tanchao on 15/12/7.
//  Copyright © 2015年 谈超. All rights reserved.
//

#import "AnimationUtil.h"
static CGFloat const kBottomLineWidth = 345.0;
@implementation AnimationUtil
+ (void)titleLabelAnimation:(UILabel *)label withView:(UIView *)view{
    [UIView animateWithDuration:1.5 animations:^{
        label.transform = CGAffineTransformIdentity;
    }];

}
+ (void)bottomPhoneLineAnimation:(NSLayoutConstraint *)constraint withView:(UIView *)view{
    constraint.constant = kBottomLineWidth;
    [UIView animateWithDuration:1.5 animations:^{
        [view layoutIfNeeded];
    }];
}

+ (void)bottomLineAnimation:(NSLayoutConstraint *)constraint withView:(UIView *)view{
    constraint.constant = constraint.constant == 0? 110 : 0;
    [UIView animateWithDuration:1.5 animations:^{
        [view layoutIfNeeded];
    }];
}


+ (void)iconViewAnimation:(UIImageView *)iconView withView:(UIView *)view{
    [UIView animateWithDuration:1.5 animations:^{
        iconView.transform = CGAffineTransformIdentity;
    }];
    
}
+ (void)tipsLabelMaskAnimation:(UIView *)view withBeginTime:(NSTimeInterval)beginTime{
    CGPathRef beginPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 0, CGRectGetHeight(view.bounds))].CGPath;
    
    CGPathRef endPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds))].CGPath;
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.path = beginPath;
    
    view.layer.mask = layer;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    animation.duration = 1;
    animation.beginTime = CACurrentMediaTime() + beginTime;
    animation.fromValue = (id)layer.path;
    animation.toValue = (__bridge id)endPath;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [layer addAnimation:animation forKey:@"shapeLayerPath"];
//    CGPathRef beginPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 0, CGRectGetHeight(view.bounds))].CGPath;
//    CGPathRef endPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds))].CGPath;
//    CAShapeLayer *maklayer = [CAShapeLayer layer];
//    maklayer.fillColor = [UIColor whiteColor].CGColor;
//    maklayer.path = beginPath;
//    view.layer.mask = maklayer;
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
//    animation.duration = 1;
//    animation.beginTime = CACurrentMediaTime() + beginTime;
//    animation.fromValue = (__bridge id _Nullable)(maklayer.path);
//    animation.toValue = (__bridge id _Nullable)(endPath);
//    animation.removedOnCompletion = NO;
//    animation.fillMode = kCAFillModeForwards;
//    [maklayer addAnimation:animation forKey:@"shapeLayerPath"];
}
+ (void)loginBtnMaskAnimation:(UIButton *)button andProgress:(CGFloat)progress{
    button.userInteractionEnabled = (progress >= 1 );
    static CAShapeLayer *layer = nil;
    if (!button.layer.mask) {
        layer = [CAShapeLayer layer];
        layer.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, CGRectGetWidth(button.bounds) * progress, CGRectGetHeight(button.bounds))].CGPath;
        layer.fillColor = [UIColor whiteColor].CGColor;
        button.layer.mask = layer;
    }else
    {
        CGPathRef path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, CGRectGetWidth(button.bounds) * progress, CGRectGetHeight(button.bounds))].CGPath;
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
        animation.duration = 0.25;
        animation.fromValue = (id)layer.path;
        animation.toValue = (__bridge id)path;
        animation.removedOnCompletion = YES;
        [layer addAnimation:animation forKey:@"shapeLayerPath"];
        layer.path = path;
        
    }
}
@end
